package cn.kgc.springcloud.service.impl;

import cn.kgc.springcloud.client.CityClient;
import cn.kgc.springcloud.client.ClazzClient;
import cn.kgc.springcloud.client.GroupClient;
import cn.kgc.springcloud.client.TagClient;
import cn.kgc.springcloud.dto.StudentDTO;
import cn.kgc.springcloud.entity.*;
import cn.kgc.springcloud.mapper.StudentMapper;
import cn.kgc.springcloud.mapper.StudentTagMapper;
import cn.kgc.springcloud.service.StudentService;
import cn.kgc.springcloud.util.DateUtil;
import cn.kgc.springcloud.vo.StudentVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService{


    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClazzClient clazzClient;

    @Autowired
    private GroupClient groupClient;

    @Autowired
    private CityClient cityClient;

    @Autowired
    private StudentTagMapper studentTagMapper;

    @Autowired
    private TagClient tagClient;

    @Override
    public List<StudentVO> getList() {

        List<Student> students = studentMapper.selectList(null);

        List<StudentVO> collect = students.stream().map(student -> {

            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);

            Integer clazzid = student.getClazzid();
            // 根据班级id  调用班级服务 查询班级详细信息
            Clazz clazz = clazzClient.getById(clazzid);
            studentVO.setClazz(clazz);

            // 查询小组详细信息
            Integer groupid = student.getGroupid();
            Group group = groupClient.getById(groupid);
            studentVO.setGroup(group);

            // 根据城市id  查询城市信息
            Integer cityid = student.getCityid();
            City city = cityClient.getById(cityid);
            studentVO.setCity(city);

            //标签信息
            Integer studentId = student.getId();
            LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
            lambda.eq(StudentTag::getSid,studentId);
            List<StudentTag> studentTags = studentTagMapper.selectList(lambda);
            List<Integer> tagIds = studentTags.stream().map(studentTag -> studentTag.getTid()).collect(Collectors.toList());

            List<Tag> tags = tagIds.stream().map(tagId -> {
                Tag tag = tagClient.getById(tagId);
                return tag;
            }).collect(Collectors.toList());

            studentVO.setTags(tags);

            return studentVO;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public StudentVO add(StudentDTO studentDTO) {
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO,student);
        // 获取学生生日
        Date bir = student.getBir();
        // 属相
        String year = DateUtil.getYear(bir);
        student.setAttr(year);
        //年龄
        int age = DateUtil.getAge(bir);
        student.setAge(age);
        // 获取星座
        String constellation = DateUtil.getConstellation(bir);
        student.setStarts(constellation);

        studentMapper.insert(student);

        // 获取标签id
        Integer[] tagIds = studentDTO.getTagIds();
        Integer studentId = student.getId();

        for (Integer tagId : tagIds) {
            StudentTag studentTag = new StudentTag();
            studentTag.setSid(studentId);
            studentTag.setTid(tagId);
            studentTagMapper.insert(studentTag);
        }


        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student, studentVO);


        Integer clazzid = student.getClazzid();
        // 根据班级id  调用班级服务 查询班级详细信息
        Clazz clazz = clazzClient.getById(clazzid);
        studentVO.setClazz(clazz);

        // 查询小组详细信息
        Integer groupid = student.getGroupid();
        Group group = groupClient.getById(groupid);
        studentVO.setGroup(group);

        // 根据城市id  查询城市信息
        Integer cityid = student.getCityid();
        City city = cityClient.getById(cityid);
        studentVO.setCity(city);

        //标签信息
        Integer stuId = student.getId();
        LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
        lambda.eq(StudentTag::getSid,stuId);
        List<StudentTag> studentTags = studentTagMapper.selectList(lambda);
        List<Integer> tagss = studentTags.stream().map(studentTag -> studentTag.getTid()).collect(Collectors.toList());

        List<Tag> tags = tagss.stream().map(tagId -> {
            Tag tag = tagClient.getById(tagId);
            return tag;
        }).collect(Collectors.toList());

        studentVO.setTags(tags);


        return studentVO;
    }
}




